﻿using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Models.DAO
{
    public class ShowTimeDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Get SelectList room by theaterId -> This function is used to load selectlist to combobox
        /// </summary>
        /// <param name="theaterId">TheaterId</param>
        /// <returns>List of room</returns>
        /// <author>vuongvt</author>
        public SelectList GetSelectListShowTimeByTheater(int theaterId)
        {
            try
            {
                IEnumerable<SelectListItem> listShowTime = new List<SelectListItem>();

                listShowTime = (from showtime in _entities.ShowTimes
                                where showtime.TheaterId == theaterId
                                  && showtime.IsDeleted == false
                                select showtime).AsEnumerable().Select(showtime => new SelectListItem()
                                {
                                    Value = showtime.ShowTimeId.ToString(),
                                    Text = showtime.Date.Date.ToShortDateString() + showtime.Time.ToString()
                                });
                return new SelectList(listShowTime, "Value", "Text");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get all show time information 
        /// </summary>
        /// <returns>List of ShowTimeConcertEntity</returns>
        /// <author>VuongVT</author>
        public List<ShowTimeConcertEntity> GetAllShowTime()
        {
            try
            {
                List<ShowTimeConcertEntity> listResult = new List<ShowTimeConcertEntity>();
                var resultQuery = (from showtime in _entities.ShowTimes
                                   join theater in _entities.Theaters on showtime.TheaterId equals theater.TheaterId
                                   where showtime.IsDeleted == false
                                        && theater.IsDeleted == false
                                   select new
                                   {
                                       ShowTimes = showtime,
                                       TheaterName = theater.TheaterName
                                   });

                foreach (var item in resultQuery)
                {
                    ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
                    showtimeConcert.ShowTimeId = item.ShowTimes.ShowTimeId;
                    showtimeConcert.Time = item.ShowTimes.Time;
                    showtimeConcert.Date = item.ShowTimes.Date;
                    showtimeConcert.TheaterId = item.ShowTimes.TheaterId;
                    showtimeConcert.IsActive = item.ShowTimes.IsActive;
                    showtimeConcert.IsDeleted = item.ShowTimes.IsDeleted;
                    showtimeConcert.TimeToShow = item.ShowTimes.Time.ToString(@"hh\:mm");
                    showtimeConcert.DateToShow = item.ShowTimes.Date.Date.ToShortDateString();
                    if (item.ShowTimes.IsActive == false)
                    {
                        showtimeConcert.StatusDescription = "Chưa áp dụng chiếu";
                    }
                    else
                    {
                        showtimeConcert.StatusDescription = "Đang áp dụng chiếu";
                    }
                    showtimeConcert.TheaterName = item.TheaterName;

                    listResult.Add(showtimeConcert);
                }
                return listResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list showtime by theaterId
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns>List of ShowTime</returns>
        /// <author>VuongVT</author> 
        public List<ShowTime> GetListShowTimeByTheaterId(int theaterId)
        {
            try
            {
                List<ShowTime> listShowTime = new List<ShowTime>();
                var resultQuery = from showtime in _entities.ShowTimes
                                  where showtime.TheaterId == theaterId
                                        && showtime.IsDeleted == false
                                  select showtime;
                foreach (var item in resultQuery)
                {
                    listShowTime.Add(item);
                }
                return listShowTime;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get a Showtime by ShowTimeId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns>ShowTime</returns>
        /// <author>VuongVT</author>
        public ShowTime GetShowTimeById(int showtimeId)
        {
            try
            {
                var resultQuery = (from showtime in _entities.ShowTimes
                                   where showtime.ShowTimeId == showtimeId
                                        && showtime.IsDeleted == false
                                   select showtime).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add new ShowTime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int CreateShowTime(ShowTime showtime)
        {
            try
            {
                showtime.IsDeleted = false;
                showtime.IsActive = true;
                _entities.ShowTimes.Add(showtime);
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int DeleteShowTime(ShowTime showtime)
        {
            try
            {
                showtime.IsDeleted = true;
                showtime.IsActive = false;
                _entities.Entry(showtime).State = System.Data.EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>1: success -- 0: fail</returns>
        /// <author>VuongVT</author>
        public int EditShowTime(ShowTime showtime)
        {
            try
            {
                _entities.Entry(showtime).State = System.Data.EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}